#lang sicp
(define (up-split painter n)
  (if (= n 0)
      painter
      (let ((up-painter (up-split painter (- n 1))))
        (below painter
               (beside up-painter up-painter)))))

(define (split op1 op2)
  (define (recur painter n)
    (if (= n 0)
        painter
        (let ((next-painter (recur (painter (- n 1)))))
          (op1 painter
               (op2 next-painter next-painter)))))
  recur)

;; 2.46
(define (add-vect v1 v2)
  (make-vect
   (+ (xcord-vect v1) (xcord-vect v2))
   (+ (ycord-vect v1) (ycord-vect v2))))
(define (sub-vect v1 v2)
  (make-vect
   (- (xcord-vect v1) (xcord-vect v2))
   (- (ycord-vect v1) (ycord-vect v2))))
(define (scale-vect v scalar)
  (make-vect
   (* scalar (xcord-vect v))
   (* scalar (ycord-vect v))))

;;2.47
(define (make-frame origin edge1 edge2)
  (list origin edge1 edge2))
(define (make-frame2 origin edge1 edge2)
  (cons origin (cons edge1 edge2)))

(define (origin-frame f) (car f))
(define (edge1-frame f) (cadr f))
(define (edge2-frame f) (caddr f))


;; 2.48
(define (make-segment start end)
  (list start end))
(define (start-segment seg)
  (car seg))
(define (end-segment seg)
  (cadr seg))

;; 2.49
(define (segments->painter segments)
  (lambda (frame)
    (let ((point-to-frame (frame-coord-map frame)))
      (foreach (lambda (seg)
                 (draw-line (point-to-frame (start-segment seg))
                            (point-to-frame (end-segment seg))))
               segments))))

(define (create-segment x0 y0 x1 y1)
  (make-segment (make-vector x0 y0) (make-vector x1 y1)))
(define (create-segments seq)
  (define (seq->segment seq)
    (create-segment (car seq) (cadr seq) (caddr seq) (cadddr seq)))
  (map seq->segment seq))


(define outline (create-segments '('(0 0 0 1)
                                   '(0 0 1 0)
                                   '(1 0 1 1)
                                   '(0 1 1 1))))
(define X (create-segments '('(0 0 1 1)
                             '(1 0 0 1))))

(define diamond (create-segments '('(0.5 0 0 0.5)
                                   '(0.5 0 1 0.5)
                                   '(0.5 1 0 0.5)
                                   '(0.5 1 1 0.5))))
;;(define wave


;; 2.50
(define (flip-horiz painter)
  (let ((new-painter (transform-painter painter
                                        (make-vect 1.0 0.0)
                                        (make-vect 0.0 0.0)
                                        (make-vect 1.0 1.0))))
    (lambda (frame)
      (new-painter frame))))
(define (rotate-180 painter)
  (let ((new-painter (transform-painter painter
                                        (make-vect 1.0 1.0)
                                        (make-vect 0.0 1.0)
                                        (make-vect 1.0 0.0))))
    (lambda (frame)
      (new-painter frame))))
(define (rotate-270 painter)
  (let ((new-painter (transform-painter painter
                                        (make-vect 0.0 1.0)
                                        (make-vect 0.0 0.0)
                                        (make-vect 1.0 1.0))))
    (lambda (frame)
      (new-painter frame))))

;; 2.51

(define (below p1 p2)
  (let ((new-p1 (transform-painter p1
                                   (make-vect 0.0 0.0)
                                   (make-vect 1.0 0.0)
                                   (make-vect 0.0 0.5)))
        (new-p2 (transform-painter p2
                                   (make-vect 0.0 0.5)
                                   (make-vect 1.0 0.5)
                                   (make-vect 0.0 1.0))))
    (lambda (frame)
      (new-p1 frame)
      (new-p2 frame))))

(define (below2 p1 p2)
  (rotate-90
   (beside (rotate-270 p1)
           (rotate-270 p2))))
                 